<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
      xmlns:jsf="http://xmlns.jcp.org/jsf">
    <h:head>
    </h:head>

    <h:body>
        <ui:composition template="/dataverse_template.xhtml">
            <ui:param name="pageTitle" value="#{TemplatePage.editMode == 'CREATE' ? bundle['dataset.manageTemplates.createBtn'] : TemplatePage.template.name} - #{TemplatePage.template.dataverse.name}"/>
            <ui:param name="dataverse" value="#{TemplatePage.template.dataverse}"/>
            <ui:param name="template" value="#{TemplatePage.template}"/>
            <ui:param name="showMessagePanel" value="#{true}"/>
            <ui:define name="body">
                <f:metadata>
                    <f:viewParam name="id" value="#{TemplatePage.templateId}"/>
                    <f:viewParam name="ownerId" value="#{TemplatePage.ownerId}"/>
                    <f:viewParam name="editMode" value="#{TemplatePage.editMode}"/>
                    <f:viewAction action="#{dataverseSession.updateLocaleInViewRoot}"/>
                    <f:viewAction action="#{TemplatePage.init}"/>
                    <f:viewAction action="#{dataverseHeaderFragment.initBreadcrumbs(TemplatePage.template.dataverse, (TemplatePage.editMode == 'CREATE' ? bundle['dataset.manageTemplates.createBtn'] : TemplatePage.template.name))}"/>
                </f:metadata>
                <h:form id="templateForm">
                    <p:focus context="templateForm"/>
                    <!-- Header / Button Panel -->
                    <ui:fragment rendered="#{empty TemplatePage.editMode}">
                        <!-- View mode -->
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="templateNameStatic">
                                    #{bundle['dataset.template.name.tip']}
                                </label>
                                <div class="col-sm-5">
                                    <p class="form-control-static">
                                        <h:outputText id="templateNameStatic" value="#{TemplatePage.template.name}"/>
                                    </p>
                                </div>
                            </div>
                        </div>

                        <h:outputLink styleClass="btn btn-default" value="/manage-templates.xhtml?dataverseId=#{TemplatePage.ownerId}">
                            <h:outputText value="#{bundle['dataset.template.returnBtn']}"/>
                        </h:outputLink>
                        
                        <h:outputText value="#{TeplatePage.template.name}"/>
                        <p:tabView id="tabView" widgetVar="content" activeIndex="#{TemplatePage.selectedTabIndex}">
                            <p:tab id="metadataMapTab" title="#{bundle['file.dataFilesTab.metadata.header']}"
                                   rendered="#{empty TemplatePage.editMode or TemplatePage.editMode == 'METADATA'}">
                                <ui:include src="metadataFragment.xhtml">
                                    <ui:param name="datasetPage" value="false"/>
                                    <ui:param name="templatePage" value="true"/>
                                    <ui:param name="editMode" value="#{!empty TemplatePage.editMode ? 'METADATA' : ''}"/>
                                    <ui:param name="metadataBlocks" value="#{!empty TemplatePage.editMode ? TemplatePage.template.metadataBlocksForEdit.entrySet().toArray() : TemplatePage.template.metadataBlocksForView.entrySet().toArray()}"/>   
                                </ui:include>
                            </p:tab>
                                <p:tab id="termsTab" title="#{bundle['file.dataFilesTab.terms.header']}"
                                   rendered="#{empty TemplatePage.editMode or TemplatePage.editMode == 'LICENSE' or TemplatePage.editMode == 'LICENSEADD'}}">
                                    <ui:include src="dataset-license-terms.xhtml" >
                                        <ui:param name="editMode" value="#{!empty TemplatePage.editMode ? 'LICENSE': ''}"/>
                                        <ui:param name="termsOfUseAndAccess" value="#{TemplatePage.template.termsOfUseAndAccess}"/>
                                        <ui:param name="datasetPage" value="false"/>               
                                    </ui:include>
                                </p:tab>                           
                        </p:tabView>
                    </ui:fragment>
                    <!-- Tabs -->
                    <ui:fragment rendered="#{!empty TemplatePage.editMode}">
                        <div class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label" for="templateName">
                                    #{bundle['dataset.manageTemplates.tab.header.templte']} 
                                    <span class="glyphicon glyphicon-asterisk text-danger" title="#{bundle.requiredField}"/>
                                    <span class="glyphicon glyphicon-question-sign tooltip-icon"
                                          data-toggle="tooltip" data-placement="auto right" data-container="body" data-original-title="#{bundle['dataset.template.name.title']}"></span>
                                </label>
                                <div class="col-sm-5">
                                    <p:inputText id="templateName" styleClass="form-control" value="#{TemplatePage.template.name}"/>
                                    <p:message for="templateName"/>
                                </div>
                                
                            </div>
                            <div class="form-group">
                                <div class="col-sm-12">
                                    <p class="form-control-static">
                                        <span class="glyphicon glyphicon-asterisk text-danger"/> <h:outputText value="#{bundle['template.asterisk.tip']}"/>
                                    </p>
                                </div>
                            </div>
                        </div>
                        <ui:fragment rendered="#{TemplatePage.editMode == 'METADATA' or TemplatePage.editMode == 'CREATE'}">
                            <ui:include src="metadataFragment.xhtml" >
                                <ui:param name="datasetPage" value="false"/>
                                <ui:param name="templatePage" value="true"/>
                                <ui:param name="editMode" value="#{!empty TemplatePage.editMode ? 'METADATA' : ''}"/>
                                <ui:param name="metadataBlocks" value="#{!empty TemplatePage.editMode ? TemplatePage.template.metadataBlocksForEdit.entrySet().toArray() : TemplatePage.template.metadataBlocksForView.entrySet().toArray()}"/>   
                            </ui:include>  
                        </ui:fragment>
                        <ui:fragment rendered="#{TemplatePage.editMode == 'LICENSE' or TemplatePage.editMode == 'LICENSEADD'}">
                            <ui:include src="dataset-license-terms.xhtml" >
                                <ui:param name="datasetPage" value="false"/>
                                <ui:param name="templatePage" value="true"/>
                                <ui:param name="editMode" value="#{!empty TemplatePage.editMode ? 'LICENSE' : ''}"/>
                                <ui:param name="termsOfUseAndAccess" value="#{TemplatePage.template.termsOfUseAndAccess}"/>   
                            </ui:include> 
                        </ui:fragment>
                    </ui:fragment>
                    <!-- Create/Save Dataset Button Panel -->
                    <div class="button-block" jsf:rendered="#{!empty TemplatePage.editMode}">
                        <p:commandButton styleClass="btn btn-default" action="#{TemplatePage.save('Terms')}" update="@all"
                                         value="#{bundle['dataset.create.add.terms']}"
                                         rendered="#{TemplatePage.editMode == 'CREATE'}">  
                             <f:ajax onerror="window.scrollTo(0, 0)"/>
                        </p:commandButton>
                        <p:commandButton styleClass="btn btn-default" action="#{TemplatePage.save('')}" update="@all"
                                         value="#{TemplatePage.editMode == 'LICENSEADD' ? bundle['dataset.manageTemplates.saveNewTerms'] : bundle.saveChanges}"
                                         rendered="#{TemplatePage.editMode == 'METADATA' or TemplatePage.editMode == 'LICENSE' or TemplatePage.editMode == 'LICENSEADD'}">
                            <f:ajax onerror="window.scrollTo(0, 0)"/>
                        </p:commandButton>
                        <h:button styleClass="btn btn-link" value="#{bundle.cancel}" 
                                  outcome="/manage-templates.xhtml?dataverseId=#{TemplatePage.dataverse.id}"></h:button>
                    </div>
                </h:form>
            </ui:define>
        </ui:composition>
    </h:body>
</html>